/*
2009-8-14
作者:啊明
QQ:172556181
支持 ie,firefox,Opera
使用方法:
<script language="javascript" src="City.js"></script>
<select id="slprovince" name="slprovince"  onchange="selectProvince();"></select>
<select id="slcity" name="slcity"  onchange="selectArea();"></select>
<select id="slarea" name="slarea"  ></select>
1. 没有设置值<script language="javascript" >InitArea();</script>
2. 指定城市名称<script language="javascript" >InitSelect('河北省','石家庄市','长安区');</script>
3. 指定城市ID<script language="javascript" >InitSelectID('320282');</script>
*/

var browse = "";
var xmlDoc;
function loadXML() {
    //  IE
    if (window.ActiveXObject) {
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = false;
        xmlDoc.load("City.xml");
        browse = "IE";
    }
    //  Firefox,Opera
    else if (document.implementation && document.implementation.createDocument) {

        xmlDoc = document.implementation.createDocument("", "", null);
        xmlDoc.async = false;
        xmlDoc.load("City.xml");
        browse = "FF";
    }
    else {
        alert('Your browser cannot handle this script');
    }
}
loadXML();

//************  为FireFox的XMLDocument 增加 LoadXML,SelectNodes,SelectSingleNode方法 *******
if (browse != "IE") {
    XMLDocument.prototype.loadXML = function(xmlString) {
        var childNodes = this.childNodes;
        for (var i = childNodes.length - 1; i >= 0; i--)
            this.removeChild(childNodes[i]);

        var dp = new DOMParser();
        var newDOM = dp.parseFromString(xmlString, "text/xml");
        var newElt = this.importNode(newDOM.documentElement, true);
        this.appendChild(newElt);
    };

    // check for XPath implementation
    if (document.implementation.hasFeature("XPath", "3.0")) {
        // prototying the XMLDocument
        XMLDocument.prototype.selectNodes = function(cXPathString, xNode) {
            if (!xNode) { xNode = this; }
            var oNSResolver = this.createNSResolver(this.documentElement)
            var aItems = this.evaluate(cXPathString, xNode, oNSResolver,
                       XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
            var aResult = [];
            for (var i = 0; i < aItems.snapshotLength; i++) {
                aResult[i] = aItems.snapshotItem(i);
            }
            return aResult;
        }

        // prototying the Element
        Element.prototype.selectNodes = function(cXPathString) {
            if (this.ownerDocument.selectNodes) {
                return this.ownerDocument.selectNodes(cXPathString, this);
            }
            else { throw "For XML Elements Only"; }
        }
    }

    // check for XPath implementation
    if (document.implementation.hasFeature("XPath", "3.0")) {
        // prototying the XMLDocument
        XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode) {
            if (!xNode) { xNode = this; }
            var xItems = this.selectNodes(cXPathString, xNode);
            if (xItems.length > 0) {
                return xItems[0];
            }
            else {
                return null;
            }
        }

        // prototying the Element
        Element.prototype.selectSingleNode = function(cXPathString) {
            if (this.ownerDocument.selectSingleNode) {
                return this.ownerDocument.selectSingleNode(cXPathString, this);
            }
            else { throw "For XML Elements Only"; }
        }
    }
}


// ff下输出节点所有XML字符
function serializeXml(oNode) {
    var oSerializer = new XMLSerializer();
    return oSerializer.serializeToString(oNode);
}

function InitArea()
{
    //获取，引用三个下拉框
    var slprovince = document.getElementById("slprovince");
    var slcity = document.getElementById("slcity");
    var slarea = document.getElementById("slarea");

    RemoveDropDownList(slprovince);

    //获取全省节点
    var provinceNodes = xmlDoc.getElementsByTagName("province");
    if (provinceNodes.length > 0) 
	{
        for (var i = 0; i < provinceNodes.length; i++) 
		{       
             selectAdd(slprovince, provinceNodes[i].getAttribute("name"), provinceNodes[i].getAttribute("postcode"));
        }
    }	 
    //slprovince.options[18].selected=true;
    selectProvince();
}

function FFselectAdd(obj, NodeName, v) {
    var l = NodeName == "area" ? 4 : 3;
    v = v.toString().substring(0, l);
    var cNodes = xmlDoc.getElementsByTagName(NodeName);
    var postcode;
    for (i = 0; i < cNodes.length; i++) {
        postcode = xmlDoc.getElementsByTagName(NodeName)[i].getAttribute("postcode");
        if (postcode.substring(0, l) == v)
            obj.options[obj.options.length] = new Option(xmlDoc.getElementsByTagName(NodeName)[i].getAttribute("name"), postcode);
        else if (postcode.substring(0, l) > v) break;

    }
}
function selectProvince() 
{
	var slprovince = document.getElementById("slprovince");
    var provinceName = slprovince.options[slprovince.selectedIndex].text;
    var provinceNode;
    var slcity = document.getElementById("slcity");   //city
    var slarea = document.getElementById("slarea");

    RemoveDropDownList(slcity);
	
    if (browse != "IE")
        FFselectAdd(slcity, "city", slprovince.value);
    else
	{
        var provinceNode = xmlDoc.selectSingleNode("//root/province[@name='" + provinceName + "']").childNodes;
        for (var i = 0; i < provinceNode.length; i++) 
		{
            selectAdd(slcity, provinceNode[i].getAttribute("name"), provinceNode[i].getAttribute("postcode"));
        }	
	}
    selectArea();
}


function selectArea() {
    var slprovince = document.getElementById("slprovince");
    var provinceName = slprovince.options[slprovince.selectedIndex].text;	
    var slcity = document.getElementById("slcity");
    var cityName = slcity.options[slcity.selectedIndex].text;

    var slarea = document.getElementById("slarea");
	
    RemoveDropDownList(slarea);

    if (browse != "IE")
        FFselectAdd(slarea, "area", slcity.value);
    else {
        var provinceNode = xmlDoc.selectSingleNode("//root/province[@name='" + provinceName + "']").childNodes;
        var cityNode;
        if (cityName == "市辖区" || cityName == "县")
            cityNode = xmlDoc.selectSingleNode("//root/province[@name='" + provinceName + "']/city[@name='" + cityName + "']").childNodes;
		else
			cityNode = xmlDoc.selectSingleNode("//root/province/city[@name='" + cityName + "']").childNodes;

        for (var i = 0; i < cityNode.length; i++) {
            selectAdd(slarea, cityNode[i].getAttribute("name"), cityNode[i].getAttribute("postcode"));
        }
    }
}

function FFselectAdd(obj, NodeName, v) {
    var le = (NodeName == "area" ? 4 : 2);
    v= v.toString().substring(0, le);
    var cNodes = xmlDoc.getElementsByTagName(NodeName);
    var postcode;
    for (i = 0; i < cNodes.length; i++) 
	{
        postcode = xmlDoc.getElementsByTagName(NodeName)[i].getAttribute("postcode");
        if (postcode.substring(0,le) == v)
            obj.options[obj.options.length] = new Option(xmlDoc.getElementsByTagName(NodeName)[i].getAttribute("name"), postcode);
        else if (postcode.substring(0, le) > v) break;
    }
	//	alert(cNodes);
}

function selectAdd(obj, text, value) {
    obj.options[obj.options.length] = new Option(text, value);
}

function RemoveDropDownList(obj) {

    if (obj)//如果参数obj不为空 则不执行
    {
        obj.options.length = 0;
    }
}

function InitSelect(Province,City,area) {
	InitArea();
	var slprovince=document.getElementById("slprovince"); 
	for (var i=0;i<slprovince.options.length;i++)
	{
			
		if (slprovince.options[i].text==Province)
		{
			slprovince.selectedIndex=i;
			selectProvince();
			break;
		}
	}
	
	var slcity=document.getElementById("slcity"); 
	for (var i=0;i<slcity.options.length;i++)
	{
		if (slcity.options[i].text==City)
		{
			slcity.selectedIndex=i;
			selectArea();
			break;
		}
	}
	
	var slarea=document.getElementById("slarea"); 
	for (var i=0;i<slarea.options.length;i++)
	{
		if (slarea.options[i].text==area)
		{
			slarea.selectedIndex=i;
			break;
		}
	}
}

function InitSelectID(areaID)
{
	var Province=areaID.substring(0,2);
	var City=areaID.substring(0,4);
	var v;
	InitArea();	
	var slprovince=document.getElementById("slprovince"); 
	for (var i=0;i<slprovince.options.length;i++)
	{
		v=slprovince.options[i].value;
		if (v.substring(0,2) == Province)
		{
			slprovince.selectedIndex=i;
			selectProvince();
			break;
		}
	}

	var slcity=document.getElementById("slcity"); 
	for (var i=0;i<slcity.options.length;i++)
	{
		v=slcity.options[i].value;
		if (v.substring(0,4) == City)
		{
			slcity.selectedIndex=i;
			selectArea();
			break;
		}
	}
	
	var slarea=document.getElementById("slarea"); 
	slarea.value=areaID;
}
